// miniprogram/pages/index/index.js
const db = wx.cloud.database({
  env: "treeworld-2g5yvz9s899bf4a6"
})
const app = getApp()


Component({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: app.globalData.CustomBar,
    height:"40px",
    loding:true,
    type:"全部",
    active:0,
    tab: 
    [{name:"全部",id:"全部"},{name:"杂谈",id:"zt"},{name:"Vue.js",id:"Vue"},{name:"Css",id:"Css"},{name:"面试",id:"ms"},{name:"小程序",id:"xcx"},{name:"JavaScript",id:"Js"},{name:"后台框架",id:"ht"},{name:"基础",id:"Jc"},{name:"网络安全",id:"Wl"}]
  },
  created() {
    this.list()
 
  },
  properties: {
    list: {
     type: Object,
       default: ''
     }, 
     loding: {
       type:Boolean,
         default: true
       }, 
   },
  watch:  {
    'active': function(a, b) {
      console.log(a)
      wx.pageScrollTo({
        scrollTop: 0
      })
    },
  },
  methods: {
    show_xl(){
      if(this.data.height=="auto"){
        this.setData({
          height:"40px"
        })
      }else{
        this.setData({
          height:"auto"
        })
      }
  
    },
    onClick(event){
      app.globalData.type=event.detail.name
      let that=this
      wx.pageScrollTo({
        scrollTop: 0
      })
    
      that.setData({
        active:event.detail.name
      })
    
   
    },
    onChange(e) {
      this.setData({
        loding: true,
        type: e.currentTarget.dataset.type
      })


      this.triggerEvent('tab_xz', e.currentTarget.dataset.type)
      // if(e.currentTarget.dataset.type=="全部"){
      //   db.collection('press').limit(10).orderBy('add_time', 'asc').get().then(res => {
      //     console.log(res)
      //     this.setData({
      //       xw_list: res.data,
      //       loding: false,
      //     })
      //   })
      //   return false
      // }
      // db.collection('press').limit(10).where({
      //   type: e.currentTarget.dataset.type
      // }).orderBy('add_time', 'asc').get().then(res => {
      //   console.log(res)
      //   this.setData({
      //     xw_list: res.data,
      //     loding: false,
      //   })
      // })
    },
    list() {
      db.collection('press').limit(20).orderBy('browse','desc').get().then(res => {
        console.log(res)
        this.setData({
          rb_list: res.data
        })
      })
      db.collection('history').orderBy('_createTime', 'desc').get().then(res => {
        console.log(res)
        this.setData({
          ls_list: res.data
        })
      })
      db.collection('press').where({
        Top:true
      }).get().then(res => {
        console.log(res)
        this.setData({
          tz_list: res.data
        })
      })

    },
    tz: function (e) {
      console.log(e.currentTarget.dataset.id)
      wx.navigateTo({
        url: "../../pages/" + e.currentTarget.dataset.url + "?id=" + e.currentTarget.dataset.id
      })
    },
  },



})